package com.googlecode.beanfiles.utils;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.converters.BooleanConverter;
import org.apache.commons.beanutils.converters.ByteConverter;
import org.apache.commons.beanutils.converters.CharacterConverter;
import org.apache.commons.beanutils.converters.DoubleConverter;
import org.apache.commons.beanutils.converters.FloatConverter;
import org.apache.commons.beanutils.converters.IntegerConverter;
import org.apache.commons.beanutils.converters.LongConverter;
import org.apache.commons.beanutils.converters.ShortConverter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ConverterUtils {

    protected static Log log() {
        return LogFactory.getLog(ConverterUtils.class);
    }

    /**
     * Registers all converters defined within the library.
     * 
     * The default BeanUtils converters do not throw ConversionExceptions, instead returning default values. This function will also override
     * those converters to throw exceptions. For this reason, it may not be advisable to register these converters if the "return default
     * values" behavior is desired, as it is in many web contexts.
     * 
     */
    public static void registerConverters() {
        ConvertUtils.register(new ListConverter(), java.util.List.class);

        Converter converter = new IntegerConverter();
        ConvertUtils.register(converter, Integer.TYPE);
        ConvertUtils.register(converter, Integer.class);

        converter = new BooleanConverter();
        ConvertUtils.register(converter, Boolean.TYPE);
        ConvertUtils.register(converter, Boolean.class);

        converter = new FloatConverter();
        ConvertUtils.register(converter, Float.TYPE);
        ConvertUtils.register(converter, Float.class);

        converter = new DoubleConverter();
        ConvertUtils.register(converter, Double.TYPE);
        ConvertUtils.register(converter, Double.class);
        
        converter = new ByteConverter();
        ConvertUtils.register(converter, Byte.TYPE);
        ConvertUtils.register(converter, Byte.class);
        
        converter = new ShortConverter();
        ConvertUtils.register(converter, Short.TYPE);
        ConvertUtils.register(converter, Short.class);
        
        converter = new LongConverter();
        ConvertUtils.register(converter, Long.TYPE);
        ConvertUtils.register(converter, Long.class);
        
        converter = new CharacterConverter();
        ConvertUtils.register(converter, Character.TYPE);
        ConvertUtils.register(converter, Character.class);
    }
}
